package com.rt.gptnext.common.utils.json;

import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;

/**
 * @author gaoyiyang
 **/
@AllArgsConstructor
public class FieldLengthSplitFilter implements com.alibaba.fastjson2.filter.ValueFilter {

    public static final FieldLengthSplitFilter INSTANCE = new FieldLengthSplitFilter(1000);

    private final int maxLen;

    @Override
    public Object apply(Object object, String name, Object value) {
        if (!(value instanceof String)) {
            return value;
        }
        String str = (String) value;
        if (StrUtil.length(str) > maxLen) {
            return str.substring(0, maxLen) + "...";
        }
        return str;
    }
}
